<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class TopTagsWidget extends Widget
{

    /**
     *
     * @param $data = array('expire' => 30, 'limit'=> 10, 'type' => 'popular');
     */
    public function render($data)
    {
        $expire = empty($data['expire']) ? 30 * 60 : intval($data['expire'] * 60);
        $limit = empty($data['limit']) ? 10 : intval($data['limit']);
        $types = array('popular', 'recent');

        if (empty($data['type']))
        {
            $key = array_rand($types);
            $type = $types[$key];
            $data['type'] = $type;
        }

        $name = 'cache_'.$type.'_tags';
        $tags = S($name);

        if(empty($tags))
        {
            $Tag = M('Tag');
            $where = array('status' => 1);

            if ('popular' == $type)
            {
           	    $order = array('question_count' => 'desc');
            }
            elseif ('recent' == $type)
            {
            	$order = array('create_time' => 'desc');
            }
            $tags = $Tag->where($where)->order($order)->limit($limit)->select();
            S($name, $tags, $expire);
        }
        $data['tags'] = $tags;

        $tplFile = TEMPLATE_PATH .'/Home/Widget_topTags.html';
        $content = $this->renderFile($tplFile, $data);
        return $content;
    }

}

/* End of file TopTagsWidget.class.php */
/* Location: ./App/Lib/Widget/TopTagsWidget.class.php */
